﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;

namespace FB.Annotations
{
    [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
    public class PrecisionAttribute : Attribute
    {
        public byte Precision { get; set; }
        public byte Scale { get; set; }

        public PrecisionAttribute(byte precision, byte scale)
        {
            this.Precision = precision;
            this.Scale = scale;
        }


        public static void ConfigureModelBuilder(DbModelBuilder modelBuilder)
        {
            modelBuilder
                .Properties()
                .Where(x => x.GetCustomAttributes(false).OfType<PrecisionAttribute>().Any())
                .Configure(c => c.HasPrecision(
                    c.ClrPropertyInfo.GetCustomAttributes(false).OfType<PrecisionAttribute>().First().Precision,
                    c.ClrPropertyInfo.GetCustomAttributes(false).OfType<PrecisionAttribute>().First().Scale
                    ));
        }

    }
}
